//
//  LD_Extension.m
//  pukka-ios
//
//  Created by lidi on 16/5/3.
//  Copyright © 2016年 Pukka Inc. All rights reserved.
//

#import "LDExtension.h"
#import <CoreServices/CoreServices.h>

@implementation LDExtension

+ (BOOL)isNotchScreen {
    BOOL isNotchScreen = NO;
    if (UIDevice.currentDevice.userInterfaceIdiom != UIUserInterfaceIdiomPhone) {//判断是否是手机
        return isNotchScreen;
    }
    if (@available(iOS 11.0, *)) {
        UIWindow *mainWindow = [self appKeyWindow];
        if (mainWindow.safeAreaInsets.bottom > 0.0) {
            isNotchScreen = YES;
        }
    }
    return isNotchScreen;
}

+ (void)mimeTypeWithRequestPath:(NSString *)path completion:(void *(^_Nullable)(NSString *))block {
    [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL fileURLWithPath:path] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        //MIMEType = 大类型/小类型
        block(response.MIMEType);
    }] resume];
}
+(void)archiveObject:(id)object withFileName:(NSString *)fileName{
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *folderPath = [DocumentPath stringByAppendingString:@"/archiveObj"];
    NSString *filePath = [folderPath stringByAppendingString:[NSString stringWithFormat:@"/%@",fileName]];
    
    NSError *error;
    // 添加文件夹
    if (![fileManager fileExistsAtPath:folderPath]){
        [fileManager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:&error];
    }
    
//    传nil表示删除文件
    if (object) {
        BOOL success = NO;
        NSData *archiveData = [NSKeyedArchiver archivedDataWithRootObject:object requiringSecureCoding:YES error:&error];
        if (archiveData == nil || error) {
            NSLog(@"归档失败:%@",error);
        } else {
            success = [archiveData writeToFile:filePath atomically:YES];
        }
        if (success) {
            NSLog(@"保存%@成功",fileName);
        }
    }else{
        if ([fileManager fileExistsAtPath:filePath]) {
            [fileManager removeItemAtPath:filePath error:&error];
        }
    }
    if (error) {
        NSLog(@"archiveObject:%@ error:%@",object,error);
    }
}
+(id)unArchiveObjectWithFileName:(NSString *)fileName classes:(NSArray<Class> *)classes{

    NSMutableSet *set = [NSMutableSet setWithArray:classes];
    [set addObjectsFromArray:@[
        [NSString class],
        [NSArray class],
        [NSDictionary class],
    ]];
    NSError *error;
    NSString *filePath = [DocumentPath stringByAppendingString:[NSString stringWithFormat:@"/archiveObj/%@",fileName]];
    NSData *objectData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:filePath] options:0 error:&error];

    id object = [NSKeyedUnarchiver unarchivedObjectOfClasses:set fromData:objectData error:&error];
    NSLog(@"unArchiveObject：%@",object);
    return object;
}

+ (void)findResponderChain:(UIResponder *)responder {
    static NSString *log = @"\n--------------------------- ResponderChain ----------------------------\n\n";
    if (responder.nextResponder) {
        log = [log stringByAppendingString:[NSString stringWithFormat:@"%@\n↓\n",responder]];
        [self findResponderChain:responder.nextResponder];
    }else{
        log = [log stringByAppendingString:[NSString stringWithFormat:@"%@\n\n--------------------------- ResponderChain ----------------------------",responder]];
        NSLog(@"%@",log);
        log = @"\n--------------------------- ResponderChain ----------------------------\n\n";
    }
}

+ (UIWindow *)appKeyWindow {
    UIWindow *window = UIApplication.sharedApplication.delegate.window;
    if (window) return window;
    if (@available(iOS 13.0, *)) {
        return UIApplication.sharedApplication.windows.firstObject;
    } else {
        return UIApplication.sharedApplication.keyWindow;
    }
}


+ (UIWindow *)topWindow {
    UIWindow *window = [[UIApplication sharedApplication].windows firstObject];
    return window;
}

+ (UIViewController *)appRootTopViewController {
    
    UIViewController *RootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    UIViewController *topVC = RootVC;
    
    while (topVC.presentedViewController) {
        
        topVC = topVC.presentedViewController;
        
    }
    
    return topVC;
}

+ (CGFloat)statusBarHeight {
    CGFloat height = 0.0;
    if (@available(iOS 13.0, *)) {
        height = [UIApplication sharedApplication].windows.firstObject.windowScene.statusBarManager.statusBarFrame.size.height;
    }else {
        height = [[UIApplication sharedApplication] statusBarFrame].size.height;
    }
    return height;
}

@end
